// SelectionSet.cpp
//
#include "SelectionSet.h"

#include "MemCheck.h"

// --------------------------------------------------------------------------------------------
SelectionSet::SelectionSet()
{
	clear();
	setPolicy(PolicyExclusive);
}

// --------------------------------------------------------------------------------------------
SelectionSet::~SelectionSet()
{};

// --------------------------------------------------------------------------------------------
// Add the desired object to the selectionlist based on current selection policy
bool SelectionSet::addObject( unsigned int objID )
{
	SelectionIndex::iterator it = _selectionIndex.find(objID);
	
	bool added = false;
	switch( _selectionPolicy )
	{
	case PolicyExclusive:	// Remove it if already present
		if( it != _selectionIndex.end() )
		{
			_selectionList.erase( it->second );
			_selectionIndex.erase( it );
		}
		else
		{
			_selectionList.push_back( objID );
			_selectionIndex[ objID ] = (--_selectionList.end());
			added = true;
		}
		break; 
	
	case PolicyAdd:
		added = true;
		if( it == _selectionIndex.end() )
		{
			_selectionList.push_back( objID );
			_selectionIndex[ objID ] = (--_selectionList.end());
		}
		break;
	}

	return added;
}

// --------------------------------------------------------------------------------------------
bool SelectionSet::removeObject( unsigned int objID )
{
	bool removed = false;

	SelectionIndex::iterator it = _selectionIndex.find(objID);
	
	if( it != _selectionIndex.end() )
	{
		_selectionList.erase( it->second );
		_selectionIndex.erase( it );

		removed = true;
	}

	return removed;
}

// --------------------------------------------------------------------------------------------
void SelectionSet::clear()
{
	_selectionList.clear();
	_selectionIndex.clear();
}